﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BreadWebAPI.Models;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Table;
using System.Configuration;

namespace BreadWebAPI.Controllers
{
    public class LikesController : ApiController
    {
        CloudStorageAccount storageAccount;
        CloudTable table;

        public LikesController()
        {
            // need to make this code execute only once
            storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);

            // Create the table client.
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

            // Create the CloudTable object that represents the "favorites" table.
            table = tableClient.GetTableReference("Favorites");

        }

        // GET api/likes
        public int Get([FromUri]string BreadDate)
        {
            int likeCount = 0;

            // Create a retrieve operation that takes a customer entity.
            TableQuery<LikesEntity> query = new TableQuery<LikesEntity>().Where(
                TableQuery.GenerateFilterCondition("BreadDate", QueryComparisons.Equal, BreadDate));

            IEnumerable<LikesEntity> likes = table.ExecuteQuery(query);
            LikesEntity likeOfDay;
            if (likes != null)
            {
                likeOfDay = likes.First<LikesEntity>();
                likeCount = likeOfDay.Count;
            }

            return likeCount;
        }

        // GET api/likes/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/likes
        public void Post([FromBody]LikesEntity value)
        {
            try
            {
                // Create a retrieve operation that takes a customer entity.
                TableQuery<LikesEntity> query = new TableQuery<LikesEntity>().Where(
                    TableQuery.GenerateFilterCondition("BreadDate", QueryComparisons.Equal, value.BreadDate));

                IEnumerable<LikesEntity> likes = table.ExecuteQuery(query);
                LikesEntity likeOfDay;
                if (likes != null)
                {
                    likeOfDay = likes.First<LikesEntity>();
                    likeOfDay.Count++;
                }
                else
                {
                    likeOfDay = new LikesEntity();
                    likeOfDay.BreadDate = value.BreadDate;
                    likeOfDay.PartitionKey = value.PartitionKey;
                    likeOfDay.RowKey = value.RowKey;
                    likeOfDay.Timestamp = value.Timestamp;
                    //likeOfDay.BreadTitle = value.BreadTitle;
                    //likeOfDay.ShortContent = value.ShortContent;
                    //likeOfDay.VerseTitle = value.VerseTitle;
                    //likeOfDay.ShortVerse = value.ShortVerse;
                    likeOfDay.Count = 1;
                }

                // Create the InsertOrReplace TableOperation
                TableOperation insertOrReplaceOperation = TableOperation.InsertOrReplace(likeOfDay);

                // Execute the operation.
                table.Execute(insertOrReplaceOperation);
            }
            catch (Exception ex)
            {
                var errorResponse = new HttpResponseMessage();
                errorResponse.StatusCode = HttpStatusCode.NotFound;
                errorResponse.Content = new StringContent(ex.ToString());
                throw new HttpResponseException(errorResponse);
            }

        }

        // PUT api/likes/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/likes/5
        public void Delete(int id)
        {
        }
    }
}
